<?php

namespace app\roomcall\controller;

use think\Db;
use think\Exception;

class Room extends Home
{
    public $noNeedLogin = [];

    /**
     *取消房间
     */
    public function close()
    {
        if ($this->request->isPost()) {
            $userInfo = $this->userInfo;
            $getData = $this->request->post();
            if (empty($getData['room_id'])) {
                $this->result('缺少参数', '', 204);
            }
            $data = Db::name('qt_room')
                ->field('id,name,status_id')
                ->where(['id' => $getData['room_id'], 'store_id' => $userInfo['store_id'], 'status_id' => '2'])
                ->find();
            if (empty($data) || $data['status_id'] != '2') {
                $this->result('房间状态有问题', '', 204);
            }
            try {
                Db::startTrans();
                //修改房间状态
                Db::name('qt_room')->where(['id' => $getData['room_id']])->update(['status_id' => '1']);
                $roomLog['room_id'] = $getData['room_id'];
                $roomLog['user_id'] = $userInfo['id'];
                $roomLog['status'] = '1';
                $roomLog['created_time'] = time();
                Db::name('qt_room_log')->insert($roomLog);
                Db::commit();
            } catch (Exception $exception) {
                Db::rollback();
                $this->result('取消失败', '', 204);
            }
            $this->result('取消成功', '', 200);
        }
        $this->result('无效的请求', '', 204);
    }

    /**
     * 设置房间颜色
     */
    public function color()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if (empty($getData['room_id']) || empty($getData['color'])) {
                $this->result('缺少参数', '', 204);
            }
            try {
                Db::startTrans();
                //修改订单状态
                Db::name('qt_room')->where('id', $getData['room_id'])->update(['color' => $getData['color']]);
                Db::commit();
            } catch (Exception $exception) {
                Db::rollback();
                $this->result('设置失败', '', 204);
            }
            $this->result('设置成功', '', 200);
        }
    }

    /**
     * 设置房间状态
     */
    public function setStatus()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if (empty($getData['room_id']) || !isset($getData['room_id'])) {
                $this->result('缺少参数', '', 204);
            }

            if (empty($getData['status']) || !isset($getData['status'])) {
                $this->result('缺少房间状态', '', 204);
            }
            if ($getData['status'] == '1') {
                if (isset($getData['user_id']) && !empty($getData['user_id'])) {
                    $insert = [
                        'store_id' => $this->userInfo['store_id'],
                        'room_id' => $getData['room_id'],
                        'user_id' => $this->userInfo['id'],
                        'clear_user_id' => $getData['user_id'],
                        'clear_time' => time(),
                        'create_time' => time(),
                    ];
                    Db::name('qt_room_clear')->insert($insert);
                } else {
                    //查看房间是否有等位
                    $waitCount = Db::name('qt_wait')->where(['id' => $getData['room_id'], 'status' => '1'])->count();
                    if ($waitCount > 0) {
                        $this->result('房间内有等待，无法取消房间！', '', 204);
                    }

                }
            }
            if ($getData['status'] == '6' || $getData['status'] == '7' || $getData['status'] == '5') {
                //查找房间是否有未结账的账单
                $project = Db::name('qt_order_project')
                    ->where(['room_id' => $getData['room_id'], 'pay_status' => '0'])
                    ->where('is_delete',2)
                    ->find();
                if (!empty($project)) {
                    $this->result('当前房间有未结账项目，请先处理商品结账！', '', 204);
                }
                //查找房间是否有未结账的账单
                $goods = Db::name('qt_product_order')
                    ->where(['room_id' => $getData['room_id'], 'pay_status' => '0'])
                    ->find();
                if (!empty($goods)) {
                    $this->result('当前房间有未结账商品，请先处理商品结账！', '', 204);
                }
            }
            try {
                Db::startTrans();
                //修改订单状态
                Db::name('qt_room')->where('id', $getData['room_id'])->update(['status_id' => $getData['status'], 'down_time' => null]);
                Db::commit();
            } catch (Exception $exception) {
                Db::rollback();
                $this->result('设置失败', '', 204);
            }
            $this->result('设置成功', '', 200);
        }
    }

    public function getRoomLog () {
        $post = $this->request->post();
        if (empty($post['room_id'])) {
            $this->result('房间ID不能为空！', '', 204);
        }
        $room_data = Db::name('qt_room')
            ->where('id', $post['room_id'])
            ->where('status_id','<>',1)
            ->find();
        if (empty($room_data)) {
            $this->result('房间还不是开房状态！', '', 204);
        }
        $data = Db::name('qt_room_log')->alias('log')
            ->field(['log.*','user.real_name'])
            ->leftJoin('qt_users user','log.user_id=user.id')
            ->where('log.room_id', $post['room_id'])
            ->order('log.id desc')
            ->find();
        $this->result('查询成功', $data, 200);
    }

}
